import { NativeModules } from "react-native";

type CryptoModule = {
  md5(text: string | undefined): string | undefined
  sha256(text: string | undefined): string | undefined
  getNetDefense(token: string, version: string): string | undefined
}

const CryptoModule = NativeModules.CryptoModule as CryptoModule;

export function md5(text: string): string | undefined {
  return CryptoModule.md5(text);
}

export function sha256(text: string): string | undefined {
  return CryptoModule.sha256(text);
}

export function getNetDefense(token: string, version: string): {
  "x-yp-nonce"?: string, "x-yp-sign"?: string
} {
  const result = CryptoModule.getNetDefense(token, version);
  if (!result) return {};
  return JSON.parse(result);
}
